﻿using CatUtils.Utils.Currency;
using CatUtils.Utils.Currency.IoUtils;
using CatUtils.Utils.SpecialUtils.Html;
using RabbitDownload.Helper.Currency;

using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace RabbitDownload.WebSite.Local.TagManger
{
    internal class LocalTagManger
    {
        /// <summary>
        /// 第一个是旧文件 ----分割那种，第二个是XML文件
        /// </summary>
        /// <param name="TagListFile"></param>
        /// <param name="NewTagListFile"></param>
        public void ZerochanMarge(string TagListFile, string NewTagListFile)
        {
            if (TagListFile == "" || NewTagListFile == "")
                return;

            List<string> OldList = FileHelper.ReadAllLine(TagListFile);
            List<string> NewList = HtmlHelper.GetHtmlValueList(FileHelper.ReadAllText(NewTagListFile), "//item");
            List<string> UntranslatedList = new List<string>();
            List<string> TranslatedList = new List<string>();
            int CompltedCount = 0;
            foreach (var item in NewList)
            {
                new PrintLog().PercentLog(NewList.Count, CompltedCount++);
                string NewRomaTag = HtmlHelper.GetFirstText(item, "//romatag");
                int TranslatedFlag = -1;//-1是当前属于初始化状态
                UntranslatedList.Add(item);
                foreach (var Olditem in OldList)
                {
                    if (Olditem.IndexOf(NewRomaTag) < 0)
                    {
                        continue;
                    }
                    if (Olditem.IndexOf("-------") < 0)
                    {
                        continue;
                    }

                    string[] TagInfo = Regex.Split(Olditem, "-------");
                    if (TagInfo.Length != 2)
                    {
                        continue;
                    }
                    String OldCntag = TagInfo[0];
                    string OldRomaTag = TagInfo[1];
                    if (OldRomaTag.Trim() == NewRomaTag.Trim())
                    {
                        Console.WriteLine("翻译成功：" + OldCntag);
                        TranslatedFlag = 1;
                        TranslatedList.Add(string.Format(LocalTagStatcivalue.TextMode, "", OldCntag, "", NewRomaTag, "", ""));
                        break;
                    }
                }
                if (TranslatedFlag == 1 && UntranslatedList.Count != 0)
                {
                    UntranslatedList.RemoveAt(UntranslatedList.Count - 1);
                }
            }
            FileHelper.AppendList(FileNameHelper.CreateFileNameAddTip(NewTagListFile, "已翻译"), TranslatedList);
            FileHelper.AppendList(FileNameHelper.CreateFileNameAddTip(NewTagListFile, "未翻译"), UntranslatedList);
        }
    }
}